﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace _180FrameWork.Common
{
    public class MyImageHelper
    {
        #region 废弃
        ///// <summary>
        ///// 等比例压缩
        ///// </summary>
        ///// <param name="mg"></param>
        ///// <param name="newSize"></param>
        ///// <returns></returns>
        //public static Image GetImageThumb(Image mg, Size newSize)
        //{
        //    double ratio = 0d;
        //    double myThumbWidth = 0d;
        //    double myThumbHeight = 0d;
        //    int x = 0;
        //    int y = 0;

        //    Bitmap bp;

        //    if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
        //    Convert.ToDouble(newSize.Height)))
        //        ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
        //    else
        //        ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
        //    myThumbHeight = Math.Ceiling(mg.Height / ratio);
        //    myThumbWidth = Math.Ceiling(mg.Width / ratio);

        //    Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
        //    bp = new Bitmap(newSize.Width, newSize.Height);
        //    x = (newSize.Width - thumbSize.Width) / 2;
        //    y = (newSize.Height - thumbSize.Height);
        //    System.Drawing.Graphics g = Graphics.FromImage(bp);
        //    g.SmoothingMode = SmoothingMode.HighQuality;
        //    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        //    Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
        //    g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

        //    return bp;
        //}
        ///// <summary>
        ///// 等比例压缩
        ///// </summary>
        ///// <param name="mg"></param>
        ///// <param name="Width"></param>
        ///// <param name="Height"></param>
        ///// <returns></returns>
        //public static Image GetImageThumb(Image mg, int Width, int Height)
        //{
        //    return GetImageThumb(mg, new Size(Width, Height));
        //}
        ///// <summary>
        ///// 等比例压缩
        ///// </summary>
        ///// <param name="mg"></param>
        ///// <param name="newSize"></param>
        ///// <returns></returns>
        //public static Bitmap GetImageThumb(Bitmap mg, Size newSize)
        //{
        //    double ratio = 0d;
        //    double myThumbWidth = 0d;
        //    double myThumbHeight = 0d;
        //    int x = 0;
        //    int y = 0;

        //    Bitmap bp;

        //    if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height)))
        //        ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
        //    else
        //        ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
        //    myThumbHeight = Math.Ceiling(mg.Height / ratio);
        //    myThumbWidth = Math.Ceiling(mg.Width / ratio);

        //    Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
        //    bp = new Bitmap(newSize.Width, newSize.Height);
        //    x = (newSize.Width - thumbSize.Width) / 2;
        //    y = (newSize.Height - thumbSize.Height);
        //    System.Drawing.Graphics g = Graphics.FromImage(bp);
        //    g.SmoothingMode = SmoothingMode.HighQuality;
        //    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        //    Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
        //    g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

        //    return bp;
        //}
        ///// <summary>
        ///// 等比例压缩
        ///// </summary>
        ///// <param name="mg"></param>
        ///// <param name="Width"></param>
        ///// <param name="Height"></param>
        ///// <returns></returns>
        //public static Bitmap GetImageThumb(Bitmap mg, int Width, int Height)
        //{
        //    return GetImageThumb(mg, new Size(Width, Height));
        //}
        #endregion

        private static bool ThumbnailCallback() { return false; }
        /// <summary>
        /// 等比例压缩
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Image Thumbnail(Image img, int width, int height)
        {
            return Thumbnail(img, new Size(width, height));
        }

        public static Image Thumbnail(Image img, Size newSize)
        {
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            return img.GetThumbnailImage(newSize.Width, newSize.Height, myCallback, IntPtr.Zero);
        }

        public static Image Thumbnail(Image img, int flag)
        {
            ImageFormat tFormat = img.RawFormat;
            Stream ms = new MemoryStream();
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arryICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;

                for (int x = 0; x < arryICI.Length; x++)
                {
                    if (arryICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arryICI[x]; break;
                    }
                }

                if (jpegICIinfo != null)
                {
                    img.Save(ms, jpegICIinfo, ep);
                }
                else
                {
                    img.Save(ms, tFormat);
                }
                ms.Position = 0;
                return Image.FromStream(ms);
            }
            //catch { return false; }
            finally { img.Dispose(); }
           
        }
        /// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="image"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string ImageToBase64(Image image, ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
        /// <summary>
        /// base64转图片
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public static Image Base64ToImage(string base64String)
        {
            //是否有base64头部
            if (base64String.StartsWith("data:image"))
            {
                base64String = base64String.Substring(base64String.IndexOf(",") + 1);
            }

            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
        /// <summary>
        /// 图片转二进制
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                if (image.RawFormat.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (image.RawFormat.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (image.RawFormat.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (image.RawFormat.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (image.RawFormat.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                else
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                byte[] buffer = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }
        /// <summary>
        /// 二进制转图片
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image BytesToImage(byte[] buffer)
        {
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                Image image = Image.FromStream(ms);
                return image;
            }
        }
        /// <summary>
        /// 旋转图片
        /// </summary>
        /// <param name="img"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static Image Rotate(Image img, RotateAngle angle)
        {
            try
            {
                if (angle == RotateAngle.left)
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                else if (angle == RotateAngle.right)
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                return img;
            }
            catch
            {
                return null;
            }
        }
    }

    public enum RotateAngle
    {
        left,
        right
    }
}